/**
 * 买卖策略
 */
let BuySell = {
	buyModel:[],
	sellModel:[],
	upday:0,
	downday:0,
	downPersent:0,
	threeDayDownPersent:0,
	topFlag:"max",
	minPrice:0,
	maxPrice:0,
  init:function(buyModel,sellModel,upday,downday,downPersent,threeDayDownPersent,topFlag,minPersent,maxPersent,middlePersent,minPrice,maxPrice){
	  this.buyModel = buyModel;
	  this.sellModel = sellModel;
	  this.upday=upday;
	  this.downday = downday;
	  this.minPrice = minPrice;
	  this.maxPrice=maxPrice;
	  
	  this.downPersent = parseFloat(downPersent);
	  this.threeDayDownPersent = threeDayDownPersent;
	  this.topFlag = topFlag?topFlag:"max";
	  this.minPersent =  parseFloat(minPersent);
	  this.maxPersent =  parseFloat(maxPersent);
	  this.middlePersent =  parseFloat(middlePersent);
	  
	  this.buyByDLGS = buyByDLGS;
	  this.buyByFiveUpTw = buyByFiveUpTw;
	  this.buyByRedUp = buyByRedUp;
	  this.buyByUpDownDay = buyByUpDownDay;
	  this.buyByTopFive = buyByTopFive;
	  this.buyByThreeLine = buyByThreeLine;
	  this.buyByKDJ = buyByKDJ;
	  this.buyByMinPrice = buyByMinPrice;
	  
	  this.sellByDownFive = sellByDownFive;
	  this.sellByDownPersent = sellByDownPersent;
	  this.sellByFiveDownTW = sellByFiveDownTW;
	  this.sellByGreen = sellByGreen;
	  this.sellByGreenDown = sellByGreenDown;
	  this.sellByRed = sellByRed;
	  this.sellByMaxPrice=sellByMaxPrice;
  },
	bs1: function(selYear, dates) {
	
		//买点
		let buySell = [];
		let condect = {
			time: 0,
			buyPrice: 0
		};
		let buyList = [];
		let kdjList = this.getKDJ(selYear);
		for (let i = 6; i < selYear.length; i++) {
			   
			     let str = "";
				 let flag = true;
				this.buyModel.forEach(key=>{
					let item = {};
					if(key=="buyByKDJ"){
						item = this[key](selYear,i,condect,buySell,dates,kdjList);
					}else{
						item = this[key](selYear,i,condect,buySell,dates);
					}
					str+=item.str;
				   if(!item.flag){
					   flag = false;
				   }
				});
				if(flag){
					let item=this.vo(selYear[i]);
					this.buyHandle(str,condect,item,buySell,dates[i]);
				};
				
		         flag=true;
				 str="";
				 this.sellModel.forEach(key=>{
						let item =  this[key](selYear,i,condect,buySell,dates);
					 str+=item.str;
					 if(!item.flag){
						 flag = false;
					 }
				 });	
				 if(flag){
					 let item = this.vo(selYear[i]);
					 this.sellHandle(str,condect,item,buySell,dates[i]);
				 }
			
		    	//最高点
				let item = this.vo(selYear[i]);
			//	let yesItem = this.vo(selYear[i-1]);
					 
			//	console.log(dates[i]+":"+(this.getMid(item)>this.getMid(yesItem))+"s;");
			    condect.buyPrice = condect.buyPrice < parseFloat(item[this.topFlag]) ? parseFloat(item[this.topFlag]) : condect.buyPrice;
		}
		return buySell;
	},	
	
	buyHandle(str,condect,item,buySell,dates){
		condect.time++;
		condect.buyPrice = item.close;
		var node = {};
		node.value = str+"\n" + item.close;
		node.xAxis = dates;
		node.yAxis = item.close;
		node.itemStyle = {
			color: "rgba(255,0,0,0.5)"
		};
		buySell.push(node);
	},
	sellHandle(str,condect,item,buySell,dates){
		condect.time--;
		var node = {};
		node.value = str +"\n"+ item.close+"\n"+condect.buyPrice;
		node.xAxis = dates;
		node.yAxis =item.close;
		node.itemStyle = {
			color: "rgba(0,255,0,0.5)"
		};
		buySell.push(node);
	},
	preUpDay(day,arr,idx){
		for(let i=0;i<day;i++){
			let item = this.vo(arr[idx-(i+1)]);
		    let yesItem = this.vo(arr[idx-(i+2)]);
			 let isBiggerYesterday = this.getMid(item)<this.getMid(yesItem);
			if(isBiggerYesterday){
				return false;
			}
		}
		return true;
	},
	preDownDay(day,arr,idx){
		for(let i=0;i<day;i++){
			let item = this.vo(arr[idx-(i+1)]);
		    let yesItem = this.vo(arr[idx-(i+2)]);
			 let isBiggerYesterday = this.getMid(item)>this.getMid(yesItem);
			if(isBiggerYesterday){
				return false;
			}
		}
		return true;
	},
	vo:function(arr){
		return{
			open:parseFloat(arr[0]),
			close:parseFloat(arr[1]),
			max:parseFloat(arr[2]),
			min:parseFloat(arr[3])
		}
	},
	isSmallPower:function(upDay,downDay,arr,idx){
		let total = upDay + downDay;
		let pw = parseFloat(arr[idx][4]);
		
		for (let i = 1; i < 4; i++) {
			   let pwTemp  =  parseFloat(arr[idx-i][4]);
			   if(pw>pwTemp){
				   return false;
			   }
		} 
		return true;
	},
	upDowDay: function(upDay, downDay, arr, idx) {
		let total = upDay + downDay;
		for (let i = 0; i < total; i++) {
			
			   let item  = this.vo(arr[idx-i]);
			   let yesItem = this.vo(arr[idx-i-1]);
			   
			    let isRed=item.open <= item.close;//收红
				let isSmallYesterday = item.close>yesItem.close;//今天收盘价大于昨天收盘
				
				let isSmallMidYes =this.getMid(item)>	this.getMid(yesItem);//今天中值大于昨天中值
				
				//跌的日子计算
                 if(upDay>i && !isSmallMidYes){
					 return false;
				 }
				 //升的日子统计
				 if(upDay<=i && isSmallMidYes){
					 return false;
				 }
		}
		
		return true;
	},
	getPJ:function(day,datas){
		let dataPJ=[];
		for(let i = 0;i<datas.length;i++){
			if(i<(day-1)){
				dataPJ.push("-");
			}else{
				let sum=0;
				for(let j=0;j<day;j++){
					sum+=parseFloat(datas[i-j][1]);
				}
				dataPJ.push(sum/day);
			}
			
		}
		return dataPJ;
	},
	getMid:function(zu){
		 return (zu.min+zu.max)*0.5;
	},
	getKDJ:function(selYear){
		return KDJ.getKDJ(selYear,9);
	}
}
